package com.reggie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.dto.DishDto;
import com.reggie.mapper.DishMapper;
import com.reggie.pojo.Dish;
import com.reggie.pojo.Employee;
import com.reggie.service.DishService;
import com.reggie.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;

    @Override
    public PageUtil<Dish> getEmployeeList(Integer page, Integer pageSize, String name) {
        PageUtil<Dish> dishPageUtil = new PageUtil<>();
        PageHelper.startPage(page, pageSize);

        List<Dish> dishList = dishMapper.findAll(page, pageSize, name);
        if (dishList==null) return null;

        Page<Dish> dishPage = (Page<Dish>) dishList;
        dishPageUtil.setTotal(dishPage.getTotal());
        dishPageUtil.setRecords(dishPage.getResult());
        return dishPageUtil;
    }

    @Override
    public boolean delete(Long id, Long userId) {
        if (dishMapper.delete(id, userId)==1) return true;
        return false;
    }

    @Override
    public boolean add(Dish dish) {
        if (dishMapper.add(dish)) return true;
        return false;
    }

    @Override
    public boolean edit(Dish dish) {
        if (dishMapper.edit(dish)==1) return true;
        return false;
    }

    @Override
    public DishDto find(Long id) {
        DishDto dishDto = dishMapper.find(id);
        if (dishDto==null) return null;
        return dishDto;
    }

    @Override
    public boolean editStatus(Long id, Integer status, Long userId) {
        if(dishMapper.editStatus(id, status, userId)==1) return true;
        return false;
    }

    @Override
    public List<DishDto> findByCategoryOrName(Long categoryId, String name, Integer status) {
        List<DishDto> dishDtos = dishMapper.findfindByCategoryOrName(categoryId, name, status);
        return dishDtos;
    }
}
